﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MusicAcademy
{
    public partial class frmClass : Form
    {
        int recordID = -1;

        public frmClass()
        {
            InitializeComponent();
        }

        private void RefreshGrid()
        {
            this.classTableAdapter.Fill(this.musicAcademyDataSet.Class, Program.academyID, "");
        }

        private void frmClass_Load(object sender, EventArgs e)
        {
            if (UserManager.HasAccess(this.Name, "View") == false)
            {
                MessageBox.Show("شما اجازه ی دیدن این فرم را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Close();
            }
            RefreshGrid();
            grdClass.ClearSelection();
            txtNumber.Focus();
        }

        private void btnCancell_Click(object sender, EventArgs e)
        {
            txtNumber.Clear();
            txtFloor.Clear();
            txtPropertise.Clear();
        }

        private bool IsValid()
        {
            bool valid = true;

            if (txtNumber.Text.Trim() == "")
                valid = Globals.SetError(txtNumber);
            else
                Globals.SetValid(txtNumber);

            if (Globals.IsInteger(txtFloor.Text) == false)
                valid = Globals.SetError(txtFloor);
            else
                Globals.SetValid(txtFloor);

            return valid;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
            SqlCommand com = new SqlCommand();
            SqlDataReader dreader;

            if (IsValid() == false)
            {
                return;
            }
            if (recordID == -1)
            {
                if (UserManager.HasAccess(this.Name, "Insert") == false)
                {
                    MessageBox.Show("شما اجازه ی اضافه کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "insert into Class(classNumber,floor,properties,academyID, creator, createDate)values(@classNumber,@floor,@propertise,@academyID, @creator, @createDate); SELECT TOP 1ID FROM Class ORDER BY ID DESC";
                com.Parameters.AddWithValue("@classNumber", txtNumber.Text);
                com.Parameters.AddWithValue("@floor", txtFloor.Text);
                com.Parameters.AddWithValue("@academyID", Program.academyID);
                com.Parameters.AddWithValue("@propertise", txtPropertise.Text);
                com.Parameters.AddWithValue("@creator", Program.userID);
                com.Parameters.AddWithValue("@createDate", Globals.Get10CharDate());
                con.Open();
                dreader = com.ExecuteReader();
                dreader.Read();
                recordID = int.Parse(dreader["ID"].ToString());
                dreader.Close();
                con.Close();

                RefreshGrid();

                btnDelete.Enabled = true;


            }
            else
            {
                if (UserManager.HasAccess(this.Name, "Update") == false)
                {
                    MessageBox.Show("شما اجازه ی ویرایش کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "UPDATE Class SET classNumber=@classNumber,floor=@floor,properties=@properties, lastEditor=@lastEditor, editDate=@editDate WHERE ID=@ID";
                com.Parameters.AddWithValue("@classNumber", txtNumber.Text);
                com.Parameters.AddWithValue("@floor", txtFloor.Text);
                com.Parameters.AddWithValue("@academyID", Program.academyID);
                com.Parameters.AddWithValue("@properties", txtPropertise.Text);
                com.Parameters.AddWithValue("@lastEditor", Program.userID);
                com.Parameters.AddWithValue("@editDate", Globals.Get10CharDate());
                com.Parameters.AddWithValue("@ID", recordID);

                con.Open();
                com.ExecuteNonQuery();
                con.Close();

                Globals.SetValid(txtNumber);
                Globals.SetValid(txtFloor);

                RefreshGrid();
            }
            MessageBox.Show("ذخیره شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnDelete_Click_1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.MusicAcademyConnectionString);
            SqlCommand com = new SqlCommand();
            if (UserManager.HasAccess(this.Name, "Delete") == false)
            {
                MessageBox.Show("شما اجازه ی حذف کردن رکورد را ندارید !", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            if (MessageBox.Show("آیا از حذف این رکورد اطمینان دارید ؟", "حذف", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "delete from Class where ID=@id";
                con.Open();
                com.Parameters.AddWithValue("@id", recordID);
                com.ExecuteNonQuery();
                con.Close();

                recordID = -1;
                txtNumber.Text = "";
                txtFloor.Text = "";
                txtPropertise.Text = "";
                btnDelete.Enabled = false;

                Globals.SetValid(txtNumber);
                Globals.SetValid(txtFloor);

                RefreshGrid();
                MessageBox.Show("حذف شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void txtFloor_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Globals.IsDigitOrCommand(e.KeyChar) == false)
                e.Handled = true;
        }

        private void grdClass_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            recordID = int.Parse(grdClass.Rows[e.RowIndex].Cells["_ID"].Value.ToString());

            txtNumber.Text = grdClass.Rows[e.RowIndex].Cells["classNumber"].Value.ToString();
            txtFloor.Text = grdClass.Rows[e.RowIndex].Cells["floor"].Value.ToString();
            txtPropertise.Text = grdClass.Rows[e.RowIndex].Cells["properties"].Value.ToString();

            Globals.SetValid(txtNumber);
            Globals.SetValid(txtFloor);

            btnDelete.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            recordID = -1;
            txtNumber.Text = "";
            txtFloor.Text = "";
            txtPropertise.Text = "";
            btnDelete.Enabled = false;

            Globals.SetValid(txtNumber);
            Globals.SetValid(txtFloor);
        }
    }
}
